// pages/collection/collection.js
const {
  userCollectionFindAll,
  deleteCollectionProduct,
  imageBaseUrl
} = require('../../api/url.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {},
    flag: false,
    page: 0,
    loadOver: false,
    list: []
  },
  //读取收藏数据
  loadCollectionData() {
    var self = this
    const {
      user,
      page,
      loadOver,
      flag
    } = this.data
    if (self.data.loadOver) return
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: userCollectionFindAll,
      data: {
        page: page,
        size: 10,
        token: wx.getStorageSync("token"),
        loginName: user.loginname,
      },
      success: function(res) {
        wx.hideLoading()
        var newList = res.data.response.cont.products
        newList.map(i => {
          i.img = imageBaseUrl + i.fMainPicture
          i.priceTxt = i.fFixedPrice.toFixed(2)
          i.originPri = i.fBuyingPrice.toFixed(2)
        })
        self.setData({
          list: flag ? newList : self.data.list.concat(newList),
          page: self.data.page + 10
        })
        if (newList.length < 10) {
          self.setData({
            loadOver: true
          })
        }
      }
    })
  },
  //删除收藏
  deleteCollectionProduct(e) {
    const {
      user
    } = this.data
    var self = this
    var id = e.currentTarget.dataset.id
    self.setData({
      flag: true,
      page: 0,
      loadOver: false
    })
    wx.showModal({
      title: '提示',
      content: '确定删除此条收藏',
      success: function(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '正在删除',
          })
          wx.request({
            url: deleteCollectionProduct,
            data: {
              fCollectionId: id,
              fType: 1,
              token: wx.getStorageSync("token"),
              loginName: user.loginname,
              fUserId: user.id
            },
            success: function(res) {
              wx.hideLoading()
              if (res.data.code == 403) {} else if (res.data == 1) {} else if (res.data.code == 0) {
                self.loadCollectionData()
                wx.showToast({
                  title: res.data.msg,
                  icon: 'success',
                  duration: 2000
                })
                var newList = self.data.list.filter(i => i.fId != id)
                app.globalData.collections = newList.map(i => i.fId)
              }
            }
          })
        } else if (res.cancel) {
        }
      }
    })
  },
  /**
   * @method
   * @desc 跳转商品详情
   */
  goToGoodDetail(e){
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?id=' + e.currentTarget.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      user: app.globalData.userInfo
    })
    this.loadCollectionData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      flag: true,
      page: 0,
      loadOver: false
    })
    this.loadCollectionData()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.setData({
      flag: false,
    })
    this.loadCollectionData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})